﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


using p_Library;
using Telerik.WinControls.UI;


namespace LIM.forms
{
    public partial class uc_FileDinhKem : BaseUserControl
    {        
        public string Path { set; get; }

        FtpClient FtpClient;
                
        public uc_FileDinhKem()
        {
            InitializeComponent();

            base.ControlPermissionList.Add(this.radButton_Upload);
            base.ControlPermissionList.Add(this.radMenuItem_XoaBo);

            FTPInfo FTPInfo = cls_Registry.Get_FTPConnection();
            
            FtpClient = new FtpClient(FTPInfo.Server, FTPInfo.Username, FTPInfo.Password);            
        }

        private void uc_FileDinhKem_Load(object sender, EventArgs e)
        {
            try
            {
                FtpClient.MakeDir(Path);
            }
            catch { }

            FtpClient.RemotePath = Path;

            RefreshData();
        }
        private void RefreshData()
        {
            this.radGridView_DanhSachFiles.Rows.Clear();
            foreach (var item in FtpClient.GetFileList())
            {
                if (!string.IsNullOrEmpty(item.Trim()))
                    this.radGridView_DanhSachFiles.Rows.Add(false, "", item);
            }
            this.radTextBox_FilePath.Text = "";

        }
        
        private void radButton_OpenFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.radTextBox_FilePath.Text = openFileDialog1.FileName.ToString();                
            }
        }

        private void radButton_Upload_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.radTextBox_FilePath.Text.Trim()))
            {
                FileInfo FileInfo = new System.IO.FileInfo(this.radTextBox_FilePath.Text.Trim());
                if (FileInfo.Exists)
                {
                    FtpClient.Upload(this.radTextBox_FilePath.Text.Trim());

                    bool isExits=false;
                    foreach (var item in this.radGridView_DanhSachFiles.Rows)
                    {
                        if (item.Cells[2].Value.ToString().ToLower().Equals(FileInfo.Name.ToLower()))
                            isExits = true;
                    }
                    if (!isExits)
                        this.radGridView_DanhSachFiles.Rows.Add(false, "", FileInfo.Name);
                }
            }
        }

        private void radMenuItem_XoaBo_Click(object sender, EventArgs e)
        {
            this.radGridView_DanhSachFiles.EndEdit();
            if (MessageBox.Show("Chương trình sẽ xóa file đã chọn." + Environment.NewLine + "Bạn có thực sự muốn xóa hay không?", "Xóa file đã upload", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {                
                foreach (var item in this.radGridView_DanhSachFiles.Rows)
                {
                    bool isSelected = (bool)item.Cells[0].Value;
                    if (isSelected)
                    {
                        FtpClient.DeleteFile(item.Cells[2].Value.ToString());
                    }
                }
                RefreshData();
            }
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {            
            RefreshData();
        }

        private void radGridView_DanhSachFiles_CommandCellClick(object sender, EventArgs e)
        {            
            string temp_Path = System.IO.Path.GetTempPath();            
            string fileName_Selected = (sender as GridCommandCellElement).RowElement.Data.Cells[2].Value.ToString();

            string temp_FileName =  temp_Path + fileName_Selected;

            FtpClient.Download(fileName_Selected, temp_FileName);

            FileInfo fileInfo = new FileInfo(temp_FileName);
            fileInfo.Attributes = FileAttributes.Temporary;

            System.Diagnostics.Process.Start(temp_FileName);
            
        }

    }
}
